package com.loper7.base.ui;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.loper7.base.R;
import com.loper7.base.utils.SharedPreferencesUtils;
import com.loper7.base.utils.StatusBarHelper;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import cn.bingoogolapple.bgabanner.BGABanner;
import io.reactivex.functions.Consumer;

public abstract class BaseSplashActivity extends BaseActivity {

    protected BGABanner mBackgroundBanner;
    protected BGABanner mForegroundBanner;
    protected RelativeLayout relativeGuide;
    protected ImageView imageSplash;

    boolean first;

    @Override
    protected void initData() {
        first = (boolean) SharedPreferencesUtils.getParam("first", true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        processLogic();
    }

    @Override
    protected void initView() {

        mBackgroundBanner = (BGABanner) findViewById(R.id.banner_guide_background);
        mForegroundBanner = (BGABanner) findViewById(R.id.banner_guide_foreground);
        relativeGuide = (RelativeLayout) findViewById(R.id.relative_guide);
        imageSplash = (ImageView) findViewById(R.id.image_shape);

        if (first) {
            RxPermissions rxPermission = new RxPermissions(this);
            rxPermission
                    .requestEach(usePermission())
                    .subscribe(new Consumer<Permission>() {
                        @Override
                        public void accept(Permission permission) {
                            if (permission.granted) {
                                init();
                                imageSplash.setVisibility(View.GONE);
                                relativeGuide.setVisibility(View.VISIBLE);
                            } else if (permission.shouldShowRequestPermissionRationale) {
                                // 用户拒绝了该权限，没有选中『不再询问』（Never ask again）,那么下次再次启动时，还会提示请求权限的对话框
                                backHelper.backward();
                            } else {
                                // 用户拒绝了该权限，并且选中『不再询问』
                                showWarningMessage("您拒绝了相关必要权限，请前往设置->应用 打开定位权限");
                                backHelper.backward();
                            }
                        }
                    });


        } else {
            RxPermissions rxPermission = new RxPermissions(this);
            rxPermission
                    .requestEach(usePermission())
                    .subscribe(new Consumer<Permission>() {
                        @Override
                        public void accept(Permission permission) {
                            if (permission.granted) {
                                init();
                                imageSplash.setVisibility(View.VISIBLE);
                                relativeGuide.setVisibility(View.GONE);
                                Handler x = new Handler();
                                x.postDelayed(new SplashHandler(), 1500);
                            } else if (permission.shouldShowRequestPermissionRationale) {
                                // 用户拒绝了该权限，没有选中『不再询问』（Never ask again）,那么下次再次启动时，还会提示请求权限的对话框
                                backHelper.backward();
                            } else {
                                // 用户拒绝了该权限，并且选中『不再询问』
                                showWarningMessage("您拒绝了相关必要权限，请前往设置->应用 打开定位权限");
                                backHelper.backward();
                            }
                        }
                    });
        }


    }


    @Override
    protected void onResume() {
        super.onResume();
        mBackgroundBanner.setBackgroundResource(android.R.color.white);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initListener() {
        /**
         * 设置进入按钮和跳过按钮控件资源 id 及其点击事件
         * 如果进入按钮和跳过按钮有一个不存在的话就传 0
         * 在 BGABanner 里已经帮开发者处理了防止重复点击事件
         * 在 BGABanner 里已经帮开发者处理了「跳过按钮」和「进入按钮」的显示与隐藏
         */
        mForegroundBanner.setEnterSkipViewIdAndDelegate(R.id.btn_guide_enter, 0, new BGABanner.GuideDelegate() {
            @Override
            public void onClickEnterOrSkip() {
                SharedPreferencesUtils.setParam("first", false);
                next();
            }
        });
    }

    @Override
    protected void setStatusBar() {
        StatusBarHelper.setStatusBar(this, StatusBarHelper.Translucent);
    }

    class SplashHandler implements Runnable {
        public void run() {
            next();
        }
    }

    protected abstract void next();

    protected abstract void processLogic();

    protected abstract void init();

    protected abstract String[] usePermission();

}
